import 'package:flutter/material.dart';

/// 自定义滑块形状 - 圆角矩形
class RoundedRectangleSliderThumbShape extends SliderComponentShape {
  final double enabledThumbRadius;

  const RoundedRectangleSliderThumbShape({
    required this.enabledThumbRadius,
  });

  @override
  Size getPreferredSize(bool isEnabled, bool isDiscrete) {
    return Size.fromRadius(enabledThumbRadius);
  }

  @override
  void paint(
    PaintingContext context,
    Offset center, {
    required Animation<double> activationAnimation,
    required Animation<double> enableAnimation,
    required bool isDiscrete,
    required TextPainter labelPainter,
    required RenderBox parentBox,
    required SliderThemeData sliderTheme,
    required TextDirection textDirection,
    required double value,
    required double textScaleFactor,
    required Size sizeWithOverflow,
  }) {
    final Canvas canvas = context.canvas;

    // 绘制滑块圆角矩形
    final RRect rRect = RRect.fromRectAndRadius(
      Rect.fromCenter(
        center: center,
        width: enabledThumbRadius * 2.2,
        height: enabledThumbRadius * 1.8,
      ),
      Radius.circular(enabledThumbRadius * 0.8),
    );

    // // 创建一个渐变效果
    final gradient = LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: [
        Colors.white,
        Colors.white.withOpacity(0.9),
      ],
    );

    // 绘制滑块主体
    final paint = Paint()
      ..color = sliderTheme.thumbColor!
      ..style = PaintingStyle.fill
      ..shader = gradient.createShader(rRect.outerRect);

    // 绘制阴影
    canvas.drawShadow(
      Path()..addRRect(rRect),
      Colors.black.withOpacity(0.2),
      2,
      true,
    );

    // 绘制主体
    canvas.drawRRect(rRect, paint);

    // 绘制线条纹理
    final linePaint = Paint()
      ..color = Colors.grey.withOpacity(0.3)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.0;

    // 画4条横向线
    final lineHeight = enabledThumbRadius * 0.8;
    final lineSpacing = enabledThumbRadius * 0.3;
    final startY = center.dy - lineHeight / 2;

    for (int i = 0; i < 4; i++) {
      final x = center.dx - lineSpacing * 1.5 + i * lineSpacing;
      canvas.drawLine(
        Offset(x, startY),
        Offset(x, startY + lineHeight),
        linePaint,
      );
    }
  }
}
